package com.capricornus.demo.controller;

import com.capricornus.demo.common.ApplicationException;
import com.capricornus.demo.common.ResponseResult;
import com.capricornus.demo.model.Student;
import com.capricornus.demo.service.user.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public ResponseResult<List<Student>>List(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String studentId=request.getParameter("studentId");
        String studentName=request.getParameter("studentName");
        List<Student> studentList=studentService.selectList(studentId,studentName);
        return ResponseResult.ok(studentList);
    }

    @DeleteMapping(value = "/{studentId}")
    public ResponseResult<Object>delete(@PathVariable("studentId") String studentId)throws Exception{
        Student student=studentService.selectByPrimaryKey(studentId);
        if (student==null){
            throw new ApplicationException(String.format("用户[%s]不存在",studentId));
        }
        studentService.updateStudentValId(studentId);
        return ResponseResult.ok("逻辑删除成功");
    }

    @DeleteMapping(value = "delete/{studentId}")
    public ResponseResult<Object>deleteByStudentId(@PathVariable("studentId")String studentId)throws Exception{
        Student student=studentService.selectByPrimaryKey(studentId);
        if (student==null){
            throw new ApplicationException(String.format("用户[%s]不存在", studentId));
        }
        studentService.deleteByStudentId(studentId);
        return ResponseResult.ok("物理删除成功");
    }

    @PostMapping(value = "/insert")
    public ResponseResult<Object>insert(@RequestBody Student student)throws Exception{
        studentService.insert(student);
        return ResponseResult.ok("新增用户成功");
    }

    @PutMapping
    public ResponseResult<String>update(@RequestBody Student student)throws Exception{
        String studentId=student.getStudentId();
        Student student1=studentService.selectByPrimaryKey(studentId);
        if (student1==null){
            throw new ApplicationException(String.format("用户[%s]不存在", studentId));
        }
        studentService.update(student);
        return ResponseResult.ok("编辑成功");
    }

    @DeleteMapping(value = "/deleteList")
    public ResponseResult<Object>deleteList(@RequestBody Student student)throws Exception{
        for (int i=0;i<student.getStudentIdList().size();i++){
            Student student1=studentService.selectByPrimaryKey(student.getStudentIdList().get(i));
            if (student1==null){
                throw new ApplicationException("该用户不存在");
            }
            studentService.updateStudentValId(student.getStudentIdList().get(i));
        }
        return ResponseResult.ok("批量逻辑删除成功");
    }

    @DeleteMapping(value = "/deleteListId")
    public ResponseResult<Object>deleteListId(@RequestBody Student student) throws Exception{
        for (int i=0;i<student.getStudentIdList().size();i++){
            Student student1=studentService.selectByPrimaryKey(student.getStudentIdList().get(i));
            if (student1==null){
                throw new ApplicationException("该用户不存在");
            }
            studentService.deleteByStudentId(student.getStudentIdList().get(i));
        }
        return ResponseResult.ok("批量物理删除成功");
    }

    @PostMapping(value = "/findPage")
    public Object FindPage(@RequestBody Student student){
        return studentService.findPage(student);
    }
}
